/**
 * Created with JetBrains PhpStorm.
 * User: skynet
 * Date: 11.01.13
 * Time: 13:26
 * To change this template use File | Settings | File Templates.
 */


/**
 * Checking on undefined and null
 * for elements
 * @param collection
 * @private
 */

function _check_for_error(data)
{
    switch(data) {
        case undefined:
            throw new Error('One of the element is undefined');
            break;
        case null:
            throw new Error('One of the element is null');
            break;
    }
};

/**
 * Checking on undefined and null
 * for input data
 * @param data
 * @private
 */
function _check_error_for_input_data(data)
{
    switch(data) {
        case undefined:
            throw new Error('Input data is undefined');
            break;
        case null:
            throw new Error('Input data is null');
            break;
    }
}

/**
 * Check array for
 * undefined, null, string - elements
 * @param elements
 * @private
 */
function _check_array_on_error(elements)
{
    for(var i = 0; i < elements.length; i++){
        if(elements[i] === undefined){
            throw new Error('One of the elements undefined');
        } else if(elements[i] === null){
            throw new Error('One of the elements null');
        } else if(typeof(elements[i]) === 'string'){
            throw new Error('One of the elements string');
        }
    }
}

var utils =
{

    /**
     * Make simple sort for array
     * @param {Array} collection
     * @return {*|void}
     */

    "sort": function(collection){
        return   _.sortBy(collection, function(num){return num;});
    },

    /**
     * Should capitalize any array of strings to single
     * string with camel case notation
     * @param {Array} strArray
     * @return {String} string
     */

    "camelize" : function (strArray){

        if(strArray.length === 0){
            return [];
        } else if(strArray.length > 0){
            if(strArray.indexOf(null) != -1){
                throw new Error('One of the element is null');
            } else if(strArray.indexOf(undefined) != -1){
                throw new Error('One of the element is undefined');
            }
            strArray = strArray.join(' ').toLowerCase();

            return _.str.camelize(strArray);
        }
    },

    /**
     * Made trim for any string
     * @param string
     * @return {String}
     */
    trim: function(string){
        var regString = new RegExp(/^(\s|\u00A0)+|(\s|\u00A0)+$/g);
        return string.replace(regString, '');
    },

    /**
     * Calls a defined callback
     * function on each element of an array,
     * and returns an array that contains the results
     * @param func
     * @param arr
     * @return {Array}
     */
    map: function(func, arr){
        // Converts an object to an array
        if(arr.length === undefined){
            var newArray = [];
            for(i in arr){
                newArray.push(arr[i]);
            }
            var arr = newArray;
        }
        var outputData = [];

        for(var i = 0; i < arr.length; i++){
            outputData[i] = func(arr[i]);
        }
        return outputData;
    },

    /**
     * Sort an object by value of key
     * @param obj
     * @param key
     */
    groupBy: function(obj, key){
        if(key === null){
            throw new Error('Error. Sorts parameter is null');
        } else if(key === undefined){
            throw new Error('Error. Sorts parameter is undefined');
        }
        if(typeof(obj) !== 'object'){
            throw new Error('Error. Input data is not an object');
        }

        var expectedGrouping = {},
            arrayElement,
            newKey;

        for(var i = 0; i < obj.length; i++){
            arrayElement = obj[i];
            newKey = arrayElement[key];

            (expectedGrouping[newKey] === undefined)
                ? (expectedGrouping[newKey] = new Array(arrayElement))
                : expectedGrouping[newKey].push(arrayElement);
        }
        return expectedGrouping;
    }




};
/**
 * Object with delivers common utility functions
 * @type {Object}
 */
if(typeof require === "function"){
    module.exports = utils;
}